package model

import (
	"errors"
	"saleforce/sf-backend-user/constant"
	"time"
)

type DepartmentModel struct {
	Id       int64             `gorm:"primary_key;column:id;type:varchar(255)" json:"id"`
	Name     string            `gorm:"column:name;type:varchar(255)" json:"name"`
	Status   int32             `gorm:"column:status;type:tinyint" json:"status"`
	Created  time.Time         `gorm:"column:created;type:timestamp" json:"created"`
	Updated  time.Time         `gorm:"column:updated;type:timestamp" json:"updated"`
	IsDel    int32             `gorm:"column:is_del;type:tinyint" json:"is_del"`
	ParentId int64             `gorm:"column:parent_id;type:tinyint" json:"parent_id"`
	Children []DepartmentModel `gorm:"-" json:"children"`
}

func (d DepartmentModel) ListByIds(ids []int64) ([]DepartmentModel, error) {
	var lst []DepartmentModel
	tx := constant.Db.Model(d).
		Omit("is_del").
		Where("id in ? and is_del = ?", ids, 0).
		Find(&lst)
	if tx.Error != nil {
		return nil, errors.New("Database error")
	}
	return lst, nil
}

func (DepartmentModel) TableName() string {
	return "sf-department"
}
